Source for file GBFilePicker.module.php
Documentation is available at GBFilePicker.module.php
#-------------------------------------------------------------------------------
# Module : GBFilePicker (c) 2010 by Georg Busch (georg.busch@gmx.net)
# a filepicker tool for CMS Made Simple
# The projects homepage is ... ToDo ...
# CMS Made Simple is (c) 2004-2010 by Ted Kulp
# The projects homepage is: cmsmadesimple.org
# File : GBFilePicker.module.php
# Purpose: initial module class.
#-------------------------------------------------------------------------------
if(isset
($CMS_ADMIN_PAGE) &&
$CMS_ADMIN_PAGE ==
1) {
$userOps =
& $gCms->GetUserOperations();
$this->isAdmin =
$userOps->UserInGroup(get_userid(false),1);
return $this->Lang('GBFilePicker');
$path =
dirname(__FILE__
) .
DIRECTORY_SEPARATOR .
'doc' .
DIRECTORY_SEPARATOR;
if(file_exists($path .
'help_' .
$this->curlang .
'.html'))
$changelog =
dirname(__FILE__
) .
DIRECTORY_SEPARATOR .
'doc' .
DIRECTORY_SEPARATOR .
'changelog.html';
return $this->lang('admindescription');
return $this->Checkpermission('Manage GBFilePicker');
return $this->Lang('postinstall', $this->GetVersion());
return $this->Lang('postuninstall', $this->GetVersion());
return $this->Lang('confirmuninstall');
function DoAction($action, $id, $params, $returnid =
'') {
// default (image dropdown)
$this->smarty->assign('imagedropdown_text', $this->lang('imagedropdown',str_replace($gCms->config['root_url'],'',$gCms->config['uploads_url']).
'/images'));
$this->smarty->assign('imagedropdown_input', $this->CreateFilePickerInput($id,'GBFilePicker_image_dropdown','','images'));
$this->smarty->assign('imagebrowser_text', $this->lang('imagebrowser',str_replace($gCms->config['root_url'],'',$gCms->config['uploads_url']).
'/images'));
$this->smarty->assign('imagebrowser_input',$this->CreateFilePickerInput($id, 'GBFilePicker_image_filepicker','','images','','','image','browser'));
$this->smarty->assign('filedropdown_text', $this->lang('filedropdown',str_replace($gCms->config['root_url'],'',$gCms->config['uploads_url']).
'/'));
$this->smarty->assign('filedropdown_input',$this->CreateFilePickerInput($id, 'GBFilePicker_file_dropdown','','','','','file'));
$this->smarty->assign('filebrowser_text', $this->lang('filebrowser',str_replace($gCms->config['root_url'],'',$gCms->config['uploads_url']).
'/'));
$this->smarty->assign('filebrowser_input',$this->CreateFilePickerInput($id, 'GBFilePicker_file_filepicker','','','','','file','browser'));
return $this->ProcessTemplate('defaultadmin.tpl');
parent::DoAction($action, $id, $params, $returnid);
function DoEvent($originator, $eventname, &$params) {
$this->RegisterModulePlugin();
// ToDo: create parameters...
#$this->RestrictUnknownParams();
case 'content_attributes':
// ToDo: Put this in DoAction ?
// $this->DoAction('default',$id,$params,'');
$value =
$this->CleanUrl($value,false);
if(isset
($params['type']))
$fileExt =
array('jpg','jpeg','gif','png');
if(isset
($params['file_extensions']) &&
$params['file_extensions'] !=
'') {
if(isset
($params['exclude_prefix']))
$excludePrefix =
trim($params['exclude_prefix']);
if(isset
($params['exclude_sufix']))
$excludeSufix =
trim($params['exclude_sufix']);
if(isset
($params['include_prefix']))
$includePrefix =
trim($params['include_prefix']);
if(isset
($params['include_sufix']))
$includeSufix =
trim($params['include_sufix']);
if(isset
($params['show_subdirs']) &&
$this->IsTrue($params['show_subdirs']))
if(isset
($params['mode']))
if(isset
($params['dir']))
$dir =
$this->CleanUrl($params['dir'],false);
if(isset
($params['allow_none']) &&
$this->IsFalse($params['allow_none']))
if(isset
($params['lock_input']) &&
$this->IsFalse($params['lock_input']))
if(isset
($params['add_txt']))
$addTxt =
trim($params['add_txt']);
if(isset
($params['size']))
$size =
trim($params['size']);
$upload =
$delete =
$createDirs =
$this->CheckPermission('Modify Files');
$showfm =
$this->GetPreference('showfilemanagement', 0);
if(isset
($params['upload']) &&
$this->IsTrue($params['upload']) &&
$showfm) {
if(isset
($params['delete']) &&
$this->IsTrue($params['delete']) &&
$showfm) {
if(isset
($params['create_dirs']) &&
$this->IsTrue($params['create_dirs']) &&
$showfm) {
#function GetNotificationOutput($priority) {
#function GetDashboardOutput() {
* -----------------------------------------------------------------------------
* Not part of the module API
* -----------------------------------------------------------------------------
* Not part of the module api.
* Creates the filepicker input.
* @param string $id - the module id
* @param string $name - the name of the input field
* @param string $value - the preselected value
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
* @param string $addTxt - any additional text that will be added to the html text input when tag is rendered (only works with mode='browser' at the moment)
* @param string $type - 'image' or 'file'
* @param string $mode - 'dropdown' or 'browser'
* @param string $showSubDirs - set to true to show subdirs
* @param string $upload - set to true to allow file upload
* @param string $delete - set to true to allow to delete of files and empty subdirs
* @param string $createDirs - set to true to allow to create subdirs
* @param string $lockInput - set to false to allow to enter the path to the file
* @param array|string$fileExt - allowed file extensions (array or csv)
* @param array|string$excludePrefix - fileprefixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includePrefix - fileprefixes to include (array('foo','bar',...) or csv)
* @param array|string$excludeSufix - filesufixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includeSufix - filesufixes to include (array('foo','bar',...) or csv)
* @param boolean $allowNone - set to false to hide the option 'none'
* @return string - the HTML output of the filepicker
$_fileExt =
array('jpg','jpeg','gif','png');
$thumbnail =
'<br /><span id="'.
$id.
$name.
'_GBFilePickerPreviewImage" class="GBFP_preview">';
if(!$thumbURL =
$this->GetThumbnail($value, $this->GetPreference('makethumbnail',0),true))
$thumbnail =
'<img class="GBFP_thumbnail" src="'.
$gCms->config['uploads_url'] .
'/' .
$value .
'" width="'.
get_site_preference('thumbnail_width',96).
'" height="'.
get_site_preference('thumbnail_height','auto').
'" alt="'.
$value.
'" title="'.
$value.
'" /></span>';
$thumbnail =
'<img class="GBFP_thumbnail" src="'.
$thumbURL.
'" alt="'.
$value.
'" title="'.
$value.
'" />';
// ToDo: create function to manipulate head section in frontend to add js and css
$this->smarty->assign('moduleId',$id);
$input =
$this->ProcessTemplate('header.tpl');
if($mode ==
'dropdown') {
<span id="'.
$id.
$name.
'_DropdownWrapper">'.
$uploadUrl =
$this->CreateLink($id, 'filePicker', '', $this->lang('uploadfile'),array('name' =>
$name,'disable_theme'=>
true,'subdir'=>
($this->isAdmin?
$dir:
'')),'', true);
$input .=
' | <input type="button" value="'.
$this->lang('uploadfile').
'"
id="'.
$id.
$name.
'_GBFP_trigger" class="FilePicker GBFP_trigger" onclick="return false;" href="'.
$uploadUrl .
'" />';
else if($mode ==
'browser') {
$input .=
'<input id="'.
$id.
$name.
'" type="hidden" name="'.
$id.
$name.
'" value="' .
$value .
'" />';
else if($type ==
'file') {
$input .=
'<input id="'.
$id.
$name.
'" type="text"' .
($lockInput?
' disabled="disabled"':
'') .
($addTxt !=
''?
' '.
$addTxt.
' ':
'') .
($size !=
''?
' size="' .
$size .
'"':
'') .
' name="' .
$id.
$name .
'" value="' .
$value .
'" />';
<input type="button" value="'.
$this->lang('select'.
$type).
'"
id="'.
$id.
$name.
'_GBFP_trigger" class="FilePicker GBFP_trigger" onclick="return false;" href="'.
$this->CreateLink($id, 'filePicker', '', $this->lang('select'.
$type),array('name' =>
$name,'disable_theme'=>
true,'subdir'=>
($this->isAdmin?
$dir:
'')),'', true).
$input .=
' | <input type="button" value="'.
lang('none').
'" onclick="jQuery(\'#'.
$id.
$name.
'\').val(\'\'); ToggleThumbnail(\'#'.
$id.
$name.
'_GBFilePickerPreviewImage\',\'\'); return false;" />';
$_SESSION['GPFP_' .
$name] =
array(
'show_subdirs'=>
$showSubDirs,
'startdir'=>
($this->isAdmin?
'':
$dir),
'exclude_prefix'=>
$excludePrefix,
'include_prefix'=>
$includePrefix,
'exclude_sufix'=>
$excludeSufix,
'include_sufix'=>
$includeSufix,
'file_extensions'=>
$fileExt,
'create_dirs'=>
$createDirs,
'allow_none'=>
$allowNone);
if($gCms->config['debug']) {
debug_buffer(array( 'mode'=>
$mode,
'show_subdirs'=>
$showSubDirs,
'startdir'=>
($this->isAdmin?
'':
$dir),
'exclude_prefix'=>
$excludePrefix,
'include_prefix'=>
$includePrefix,
'exclude_sufix'=>
$excludeSufix,
'include_sufix'=>
$includeSufix,
'file_extensions'=>
$fileExt,
'create_dirs'=>
$createDirs,
'allow_none'=>
$allowNone),
'GBFilePicker session vars - filepicker input = "'.
$name.
'" ');
return $input.
$thumbnail;
* Not part of the module api.
* Gets the file type of a file.
* @param string $file - the path to a file
* @param string $useMime - set to true to detect file type by mime type
* @return string - the mime-type or file extension
$finfo =
finfo_open(FILEINFO_MIME_TYPE);
$fileType =
strtolower(finfo_file($finfo, $file));
* Not part of the module api.
* Create a dropdown form element containing a list of files that match certain conditions
* @param string $id - the module id
* @param string $name - the name of the input field
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
* @param string $selected - the preselected value
* @param array|string$excludePrefix - fileprefixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includePrefix - fileprefixes to include (array('foo','bar',...) or csv)
* @param array|string$excludeSufix - filesufixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includeSufix - filesufixes to include (array('foo','bar',...) or csv)
* @param array|string$fileExt - filesufixes to include (array('foo','bar',...) or csv)
* @param string $type - file or image
* @param boolean $allowNone - set to false to hide the option 'none'
* @return string - the HTML output of a select element with options
function CreateFileDropdown($id, $name, $dir =
'', $selected =
'', $excludePrefix =
'', $includePrefix =
'',
$excludeSufix =
'', $includeSufix =
'', $fileExt =
'', $type =
'', $allowNone =
true)
$files =
$this->GetFiles($dir, $excludePrefix, $includePrefix,
$excludeSufix, $includeSufix, $fileExt, $type, true);
$dropdown =
'<select name="'.
$id.
$name.
'" id="'.
$id.
$name.
'"';
$dropdown .=
' onchange="ToggleThumbnail(\'#' .
$id.
$name .
'_GBFilePickerPreviewImage\', jQuery(\'#' .
$id.
$name .
' option:selected:first\').attr(\'thumbnail\'),this.value,this.value)"';
$dropdown .=
'<option value=""';
$dropdown .=
' selected="selected"';
$dropdown .=
' thumbnail="">--- '.
lang('none').
' ---</option>';
foreach( $files as $file )
$dropdown .=
'<option value="'.
$file['rel_url'].
'"';
if($file['rel_url'] ==
$selected)
$dropdown .=
' selected="selected"';
if($file['isimage'] &&
$type ==
'image')
$dropdown .=
' thumbnail="'.
(isset
($file['thumburl']) &&
$file['thumburl']!=
''?
$file['thumburl']:
$file['fullurl']).
'"';
$dropdown .=
'>'.
$file['basename'].
'</option>';
$dropdown .=
"</select>";
* Not part of the module api.
* Return an array containing a list of files in a directory.
* Performs a non recursive search.
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
* @param array|string$excludePrefix - fileprefixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includePrefix - fileprefixes to include (array('foo','bar',...) or csv)
* @param array|string$excludeSufix - filesufixes to exclude (array('foo','bar',...) or csv)
* @param array|string$includeSufix - filesufixes to include (array('foo','bar',...) or csv)
* @param array|string$fileExt - filesufixes to include (array('foo','bar',...) or csv)
* @param string $type - file or image
* @param boolean $excludeDirs - set to true to exclude directories
function GetFiles($dir =
'', $excludePrefix =
'', $includePrefix =
'', $excludeSufix =
'',
$includeSufix =
'', $fileExt =
'', $type =
'image', $excludeDirs =
false)
return $files; // ToDo: return errormessage?
$filePickerstyle =
$this->GetPreference('filepickerstyle','both');
$_fileExt =
array('jpg','jpeg','gif','png');
while ($entry =
$d->read()) {
if ($entry[0] ==
'.' ||
(is_dir($dir.
$entry) &&
$excludeDirs))
foreach($excludePrefix as $str) {
if(startswith($file['filename'],$str)) {
foreach($includePrefix as $str) {
if(startswith($str,'thumb_')) {
if(!startswith($file['filename'],$str)) {
foreach($excludeSufix as $str) {
if(endswith($file['filename'],$str)) {
foreach($includeSufix as $str) {
if(!endswith($file['filename'],$str)) {
if(startswith($entry,'thumb_') &&
!$this->GetPreference('showthumbnailfiles',0) &&
!$includeThumb) {
$fileType =
$this->GetFileType($dir.
$entry,$this->GetPreference('usemimetype',0));
else if(!$isDir &&
$type !=
'image' &&
!in_array($file['extension'],$fileExt) &&
!empty($fileExt)) {
$file['isimage'] =
false;
$file['fullpath'] =
$dir.
$entry;
$file['fullurl'] =
$url.
$entry;
$file['rel_url'] =
trim(str_replace($gCms->config['uploads_url'],'',$file['fullurl']),'/');
$file['type'] =
$fileType;
$file['last_modifed'] =
filemtime($dir.
$entry);
if ($filePickerstyle !=
'filename') {
if ($this->GetPreference('showthumbnailfiles',0) ||
$includeThumb) {
$file['thumbnail'] =
$this->GetThumbnail($dir.
str_replace('thumb_', '', $entry), $this->GetPreference('makethumbnail',0));
$file['thumburl'] =
$this->GetThumbnail($dir.
str_replace('thumb_', '', $entry), $this->GetPreference('makethumbnail',0),true);
$file['thumbnail'] =
$this->GetThumbnail($dir.
$entry, $this->GetPreference('makethumbnail',0));
$file['thumburl'] =
$this->GetThumbnail($dir.
$entry, $this->GetPreference('makethumbnail',0),true);
$file['dimensions'] =
$imgsize[0] .
' x ' .
$imgsize[1];
$file['dimensions'] =
' ';
$info =
@stat($file['fullpath']);
$file['size'] =
$info['size'];
if($gCms->config['debug']) {
$file['debug'] =
'<br />';
foreach($file as $k=>
$v) {
$file['debug'] .=
"$k : $v<br />";
usort($files, array($this, 'SortFiles'));
* Not part of the module api.
* Replaces all multiple DIRECTORY_SEPARATOR, dots and (multiple) slashes with one single DIRECTORY_SEPARATOR to make a clean save url parameter
* @param string $path - the path to clean
* @param string $fullPath - true to prepend uploads path on return, false to return only the cleaned $path
* @return string - the clean path
function CleanPath($path, $fullPath =
true) {
$path =
trim(str_replace(array($gCms->config['uploads_path'],$gCms->config['uploads_url']), '', $path),'/'.
DIRECTORY_SEPARATOR);
$path =
preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, '/' .
$path .
'/');
$path =
$gCms->config['uploads_path'] .
$path;
return trim(preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, $path) , DIRECTORY_SEPARATOR);
* Not part of the module api.
* Replaces all multiple slashes, dots and (multiple) backslashes with one single slash to make a clean save url parameter
* @param string $url - the url to clean
* @param string $fullUrl - true to prepend uploads url on return, false to return only the cleaned $url
* @return string - the clean url
function CleanURL($url, $fullUrl =
true) {
$url =
trim(str_replace(array($gCms->config['uploads_path'],$gCms->config['uploads_url']), '', $url),'/'.
DIRECTORY_SEPARATOR);
$url =
$gCms->config['uploads_url'] .
$url;
* Not part of the module api.
* Returns the thumbnail of a given image file.
* @param string $path - the path of the file to create a thumbnail of (can be relative to the uploads dir or absolute)
* @param string $makeThumbnail - set to false if thumbnail may not be created if not exists
* @param string $urlOnly - set to true to get only the url to the thumbnail
* @return string - HTML img element or url
function GetThumbnail($path, $makeThumbnail =
true, $urlOnly =
false) {
$url =
$this->CleanUrl(str_replace($fileName,'',$path));
if(!$this->HandleFileResizing($path .
$fileName, $path .
'thumb_' .
$fileName, get_site_preference('thumbnail_width', 96), get_site_preference('thumbnail_height',96),true,false,60))
return $url .
'thumb_' .
$fileName;
return '<img class="GBFP_thumbnail" src="' .
$url .
'thumb_' .
$fileName .
'" alt="' .
str_replace($gCms->config['uploads_url'],'',$url) .
$fileName .
'" title="' .
str_replace($gCms->config['uploads_url'],'',$url) .
$fileName .
'" />';
* Not part of the module api.
* Gets the icon for a file.
* If FileManager is installed it uses FileManager icons.
* Otherwise it uses own icons.
* @param array $fileInfo - an array with info about the file ('ext'=>file extension, 'isdir'=>true/false, isimage=>true/false)
* @return string - the HTML ouput of an image
if($fileManager =
& $this->GetModuleInstance('FileManager')) {
return $fileManager->GetFileIcon($fileInfo['ext'], $fileInfo['isdir']);
return '<img class="GBFP_fileicon" src="../modules/GBFilePicker/images/dir.gif" title="" alt="" />';
else if ($fileInfo['isimage'])
return '<img class="GBFP_fileicon" src="../modules/GBFilePicker/images/images.gif" title="" alt="" />';
return '<img class="GBFP_fileicon" src="../modules/GBFilePicker/images/fileicon.gif" title="" alt="" />';
* Not part of the module api.
* Gets the icon for the file operation.
* If FileManager is installed it uses FileManager icons.
* Otherwise it uses own icons
* @param string $action - the file operation (e.g. 'delete')
* @return string - the HTML ouput of an image
if($fileManager =
& $this->GetModuleInstance('FileManager')) {
return $fileManager->GetActionIcon($action);
return '<img class="GBFP_actionicon" src="../modules/GBFilePicker/images/'.
$action.
'.gif" title="'.
$action.
'" alt="'.
$action.
'" />';
* Not part of the module api.
* Taken from TinyMCE Module (extracted from filepicker).
while ($entry =
$d->read()) {
if ($entry ==
'.') continue;
if ($entry ==
'..') continue;
* Not part of the module api.
* Taken from TinyMCE filepicker.
if ($file1['isdir'] &&
!$file2['isdir']) return -
1;
if (!$file1['isdir'] &&
$file2['isdir']) return 1;
* Not part of the module api.
* Adapted from TinyMCE filepicker.
function HandleFileResizing($source, $output, $newWidth, $newHeight, $keepAspectRatio =
true, $allowUpscaling =
false, $quality =
100) {
switch($imgData['mime']) {
$origHeight =
@imagesy($origImage);
$aspectRatio =
$origWidth /
$origHeight;
$newWidth =
floor($newWidth);
$newHeight =
floor($newHeight);
if($newWidth <=
0 &&
$newHeight >
0) { // force height
if($newHeight >
$origHeight &&
!$allowUpscaling) {
$newHeight =
$origHeight;
$newWidth =
floor($newHeight *
$aspectRatio);
else if($newHeight <=
0 &&
$newWidth >
0) { // force width
$newHeight =
$origHeight;
if($newWidth >
$origWidth &&
!$allowUpscaling) {
$newHeight =
floor($newWidth /
$aspectRatio);
else if($newHeight >
0 &&
$newWidth >
0) { // both
if($newWidth >
$origWidth &&
!$allowUpscaling) {
if($newHeight >
$origHeight &&
!$allowUpscaling) {
$newHeight =
$origHeight;
$newAspectRatio =
$newWidth /
$newHeight;
if($aspectRatio >
1 &&
$newAspectRatio <
1) { // landscape to portrait
$_tmp =
floor($newWidth /
$aspectRatio);
if($_tmp >
0 &&
$_tmp <=
$newHeight)
else if($aspectRatio <
1 &&
$newAspectRatio >
1) { // portrait to landscape
$_tmp =
floor($newHeight *
$aspectRatio);
if($_tmp >
0 &&
$_tmp <=
$newWidth)
if($newAspectRatio <
$aspectRatio) {
$_tmp =
floor($newWidth /
$aspectRatio);
if($_tmp >
0 &&
$_tmp <=
$newHeight)
else if($newAspectRatio >
$aspectRatio) {
$_tmp =
floor($newHeight *
$aspectRatio);
if($_tmp >
0 &&
$_tmp <=
$newWidth)
$newHeight =
$origHeight;
@imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
switch($imgData['mime']) {
case 'image/jpeg':
return @imagejpeg($newImage, $output, $quality);
case 'image/gif' :
return @imagegif($newImage, $output);
case 'image/png' :
return @imagepng($newImage, $output);
//todo: enable format change?
if ($format=='unchanged') {
switch($imgData['mime']) {
case 'image/jpeg': return @imagejpeg($newImage, $output, $quality);
case 'image/gif' : return @imagegif($newImage, $output);
case 'image/png' : return @imagepng($newImage, $output);
case 'jpg': return @imagejpeg($newImage, $output, $quality);
case 'gif': return @imagegif($newImage, $output);
case 'png': return @imagepng($newImage, $output);
* Not part of the module api.
* Taken from TinyMCE Module.
if (strpos($filename, '\'') !==
false) return true;
if (strpos($filename, '"' ) !==
false) return true;
if (strpos($filename, '/' ) !==
false) return true;
if (strpos($filename, '\\') !==
false) return true;
if (strpos($filename, '&' ) !==
false) return true;
if (strpos($filename, '\$') !==
false) return true;
if (strpos($filename, '+' ) !==
false) return true;
* Not part of the module api.
* Checks if a var is empty.
* If $var is an array it recursivley checks all elements.
* @param mixed $var - the var to check for empty value(s)
* @param boolean $trim - true to trim off spaces
* @param boolean $unsetEmptyIndexes - true to delete empty elements from array
* @return boolean - true if empty, false if not
function IsVarEmpty(&$var, $trim =
true, $unsetEmptyIndexes =
false) {
foreach ($var as $k=>
$v) {
else if($trim &&
trim($var) ==
'') {
* Not part of the module api.
* Removes empty elements from an array.
* (can be useful when using function explode to create the array from a csv)
* @param array $array - the array to clean up
* @return array - an array without empty elements or an empty array
foreach ($array as $k=>
$v) {
* Not part of the module api.
* Checks if a value is really meant to be "true".
* Can be usefull when checking smarty params for the value true
* @param mixed $value - the value to check
return (strtolower($value) ===
'true' ||
$value ===
1 ||
$value ===
'1' ||
$value ===
true);
* Not part of the module api.
* Checks if a value is really meant to be "false".
* Can be usefull when checking smarty params for the value false
* @param mixed $value - the value to check
return (strtolower($value) ===
'false' ||
$value ===
'0' ||
$value ===
0 ||
$value ===
false ||
$value ===
'');
Documentation generated on Sun, 05 Sep 2010 21:38:36 +0200 by phpDocumentor 1.4.0a2